package chapter8.section2;

//程序8-2：数组和List可以相互转换

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayToList {
    public static void main(String[] args) {
        String[] s = new String[]{"A", "B", "C", "D", "E"};
        List<String> list1 = Arrays.asList(s);// 将字符串数组转换成List,等号右侧返回实际是ArrayList对象，Arrays.asList返回可变的list,可以对list1修改
        List<String> list2 = List.of(s);// List.of返回不可变的list,不能对list2修改
        list1.set(1, "dsf");

		for (String l : list1) {
			System.out.println(l);
		}

//    	list2.add("dsf");
        ArrayList<String> arr = new ArrayList<>();
        arr.add("a");
        arr.add("b");
        String[] dest = arr.toArray(new String[0]); // List转换为数组，new String[0]指定返回数组的类型
        for (String e : dest)
            System.out.println(e);
    }
}
